package cn.pku.eecs.ldn.activity;

import java.util.ArrayList;
import java.util.List;

import cn.pku.eecs.ldn.R;
import cn.pku.eecs.ldn.util.ImageUtil;
import cn.pku.eecs.ldn.view.ZoomImageView;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class TestViewPagerActivity extends Activity {
	private final static String TAG = "TestViewPager";
	
	ViewPager viewPagerAlbum = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test_view_pager);
		viewPagerAlbum = (ViewPager) findViewById(R.id.viewPagerAlbum);
		
		List<View> viewList = new ArrayList<View>();
		
		ZoomImageView zoomImageView = new ZoomImageView(this);
		Bitmap bitmap = ImageUtil.loadBitmap(this, R.drawable.naeun);
		zoomImageView.setBitmap(bitmap);
		viewList.add(zoomImageView);
		
		zoomImageView = new ZoomImageView(this);
		bitmap = ImageUtil.loadBitmap(this, R.drawable.girl);
		zoomImageView.setBitmap(bitmap);
		viewList.add(zoomImageView);
		
		zoomImageView = new ZoomImageView(this);
		bitmap = ImageUtil.loadBitmap(this, R.drawable.sky);
		zoomImageView.setBitmap(bitmap);
		viewList.add(zoomImageView);
		
		//		ImageView imageView = new ImageView(this);
//		imageView.setImageResource(R.drawable.naeun);
//		viewList.add(imageView);
//		
//		imageView = new ImageView(this);
//		imageView.setImageResource(R.drawable.girl);
//		viewList.add(imageView);
//		
//		imageView = new ImageView(this);
//		imageView.setImageResource(R.drawable.sky);
//		viewList.add(imageView);
		
		viewPagerAlbum.setAdapter(new AlbumViewPager(viewList));
	}
	
	private class AlbumViewPager extends PagerAdapter {
		List<View> viewList = null;
		
		public AlbumViewPager(List<View> viewlList) {
			this.viewList = viewlList;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			container.removeView(viewList.get(position));
//			super.destroyItem(container, position, object);
		}

		@Override
		public void finishUpdate(ViewGroup container) {
			// TODO Auto-generated method stub
			super.finishUpdate(container);
		}

		@Override
		public int getItemPosition(Object object) {
			// TODO Auto-generated method stub
			return super.getItemPosition(object);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			// TODO Auto-generated method stub
			return super.getPageTitle(position);
		}

		@Override
		public float getPageWidth(int position) {
			// TODO Auto-generated method stub
			return super.getPageWidth(position);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			// TODO Auto-generated method stub
			container.addView(viewList.get(position));
			return viewList.get(position);
//			return super.instantiateItem(container, position);
		}

		@Override
		public void notifyDataSetChanged() {
			// TODO Auto-generated method stub
			super.notifyDataSetChanged();
		}

		@Override
		public void restoreState(Parcelable state, ClassLoader loader) {
			// TODO Auto-generated method stub
			super.restoreState(state, loader);
		}

		@Override
		public Parcelable saveState() {
			// TODO Auto-generated method stub
			return super.saveState();
		}

		@Override
		public void setPrimaryItem(ViewGroup container, int position,
				Object object) {
			// TODO Auto-generated method stub
			super.setPrimaryItem(container, position, object);
		}

		@Override
		public void startUpdate(ViewGroup container) {
			// TODO Auto-generated method stub
			super.startUpdate(container);
		}

		@Override
		public int getCount() {
			if(viewList == null) {
				return 0;
			}
			return viewList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}
	}
}
